/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;

import Domain.Instructor;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Min0
 */
public class DBInstructor implements IFDBInstructor 
{
       private Connection con;

    public DBInstructor()
    {
        con = DBConnection.getInstance().getDBcon();
    }

    //START private methods for class
    private Instructor BuildApartment(ResultSet results)
    {
        Instructor appObj = new Instructor();
        try
        {
            appObj.setLoginID(results.getString("loginID"));
            appObj.setName(results.getString("Name"));
            appObj.setAddress(results.getString("Address"));
            appObj.setCountry(results.getString("Country"));
            appObj.setPhoneNo(results.getString("PhoneNo"));
            appObj.setEmail(results.getString("Email"));
            appObj.setType(results.getString("Type"));
            appObj.setPricePerHour(results.getFloat("PricePerHour"));
        } catch (Exception e)
        {
            System.out.println("error in building the Instructor object\n" + e.getMessage());
        }
        return appObj;
    }

    private String BuildQuery(String wClause)
    {
        String query = "SELECT * FROM Instructor";

        if (wClause.length() > 0)
        {
            query = query + " WHERE " + wClause;
        }

        return query;
    }

    private Instructor singleWhere(String wClause)
    {
        ResultSet results;
        Instructor appObj = new Instructor();
        String query = BuildQuery(wClause);
        System.out.println(query);
        try
        {
            java.sql.Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            if (results.next())
            {
                appObj = BuildApartment(results);
                //assocaition is to be build
                stmt.close();
            }

        }//end try	
        catch (Exception e)
        {
            System.out.println("Query exception: " + e);
        }
        return appObj;
    }

    private ArrayList<Instructor> miscWhere(String wClause)
    {
        ResultSet results;
        ArrayList<Instructor> list = new ArrayList<Instructor>();

        String query = BuildQuery(wClause);

        try
        { // read the Instructor from the database
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            while (results.next())
            {
                Instructor appObj = new Instructor();
                appObj = BuildApartment(results);
                list.add(appObj);
            }//end while
            stmt.close();
        } catch (Exception e)
        {
            System.out.println("Query exception - select: " + e);
        }
        return list;
    }
    //END private methods for class

    @Override
    public void InsertInstructor(Instructor Instructor)
    {
        String query = "INSERT INTO Instructor (LoginID, Name, Address, Country, PhoneNo, Email, Type, PricePerHour) VALUES ('"
                + Instructor.getLoginID() + "','"
                + Instructor.getName() + "','"
                + Instructor.getAddress() + "','"
                + Instructor.getCountry() + "','"
                + Instructor.getPhoneNo() + "','"
                + Instructor.getEmail() + "','"
                + Instructor.getType() + "',"
                + Instructor.getPricePerHour()
                + ")";

        System.out.println("insert: " + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        }//end try	
        catch (Exception ex)
        {
            System.out.println("Insert exception in Instructor db: " + ex);
        }

    }

    @Override
    public void InsertInstructor(String LoginID, String Name, String Address, String Country, String PhoneNo, String Email, String Type, float PricePerHour)
    {
        String query = "INSERT INTO Instructor (LoginID, Name, Address, Country, PhoneNo, Email, Type, PricePerHour) VALUES ('"
                + LoginID + "','"
                + Name + "','"
                + Address + "','"
                + Country + "','"
                + PhoneNo + "','"
                + Email + "','"
                + Type + "',"
                + PricePerHour
                + ")";

        System.out.println("insert: " + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        }//end try	
        catch (Exception ex)
        {
            System.out.println("Insert exception in Instructor db: " + ex);
        }
    }

    @Override
    public Instructor GetInstructor(String LoginID)
    {
        String wClause = "LoginID = '" + LoginID + "'";
        return singleWhere(wClause);
    }

    @Override
    public ArrayList<Instructor> GetInstructorList()
    {
        return miscWhere("");
    }

    @Override
    public void UpdateInstructor(String LoginID, String Name, String Address, String Country, String PhoneNo, String Email, String Type, float PricePerHour)
    {
        String query = "UPDATE Instructor SET "
                + "Name = '" + Name + "',"
                + "Address ='" + Address + "',"
                + "Country = '" + Country + "',"
                + "PhoneNo = '" + PhoneNo + "',"
                + "Email = '" + Email + "',"
                + "Type = '" + Type + "',"
                + "PricePerHour = " + PricePerHour
                + "' WHERE LoginID = '" + LoginID + "'";
        System.out.println("UPDATE QUERY: " + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in Instructor db: " + ex);
        }
    }

    @Override
    public void DeleteInstructor(String LoginID)
    {
        String query = "DELETE FROM Instructor WHERE LoginID = '"
                + LoginID + "'";
        System.out.println(query);
        try
        { // delete from Instructor
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Delete exception in Instructor db: " + ex);
        }
    }

    @Override
    public void BookInstructor(String LoginID, boolean book)
    {
            String query = "UPDATE Instructor SET "
                + "Booked = '" + book + "'"
                + "' WHERE LoginID = '" + LoginID + "'";
        System.out.println("UPDATE QUERY: " + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in Instructor db: " + ex);
        }
    }

    @Override
    public int getMax()
    {
        int max = getMax.getMaxId("Select max(LoginID) from Instructor");
        return max;
    }
}
